package com.hrsaas.ext.spider.test;

import java.util.Set;

import com.hrsaas.ext.spider.client.HeaderFactory;
import com.hrsaas.ext.spider.core.CookieStore;
import com.hrsaas.ext.spider.core.HttpClient;
import com.hrsaas.ext.spider.test.handler.HttpClientFactory;
import com.hrsaas.ext.spider.test.handler.LoginLGHandler;
import com.hrsaas.ext.spider.test.mybatis.model.ResumeSpider;

import io.netty.handler.codec.http.Cookie;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.util.internal.StringUtil;

public class LaGouNewSpider extends LoginLGHandler {

	public LaGouNewSpider(ResumeSpider model) {
		super(model);
	}

	/**
	 * 发起登录请求
	 */
	public void load() {
		HttpClient client = HttpClientFactory.getClient();
		ResumeSpider spider = getModel();
		String cookies = spider.getCookies();
		String headers = spider.getHeaders();
		if (!StringUtil.isNullOrEmpty(cookies) && !StringUtil.isNullOrEmpty(headers)) {
			Set<Cookie> cks = io.netty.handler.codec.http.CookieDecoder.decode(cookies);
			CookieStore store = new CookieStore();
			for (Cookie ck : cks) {
				store.add(ck);
			}
			HttpHeaders hs = HeaderFactory.extract(headers);
			reload(client, store, hs);
		} else {
			client.get().
			setURL("https://easy.lagou.com/can/new/index.htm?can=true&stage=NEW&needQueryAmount=true").
			addHeaders(HeaderFactory.EASY_INDEX).
			execute(this);
		}
	}
	
}
